require.config({
	shim: {	　　　　　　
		"bootstrap": {	// bootstrap依赖jquery库
			deps: ['jquery'],
			exports: 'bootstrap'
		},
		'handlebars': {　　　　　　　　
			exports: 'Handlebars'　　　　　　
		}　　　　
	},
	paths: {　　　　　　
		//工具模块
		'less': 'tools/less.min',
		'jquery': 'tools/jquery.min',
		'bootstrap': 'tools/bootstrap.min',
		'handlebars': 'tools/handlebars',

		//配置模块
		'config': './config',
		"appConfig":"./appConfig",
		'showBigImg':'modules/showBigImg',
		'top_bar': 'modules/top_bar',
		'problem': 'modules/problem',
		'ask': 'modules/ask',
		'modal_template': 'modules/modal_template',
		'cookie': 'modules/cookie',
		'check_log': 'modules/check_log',
		'login': 'modules/login',
		'other_log': 'modules/login',
		'reg': 'modules/reg',
		'user': 'modules/user',　　
		'setCenterImgBox':'modules/setCenterImgBox',
		'studioProblem':'modules/studioProblem',
		'jquery.mousewheel':"modules/jquery.mousewheel",
		'uploadFile':'modules/uploadFile'
	}
});
require(['jquery','uploadFile','handlebars'], function ($,uploadFile,Handlebars){
	var imgFiles=[],iconUrl;
	function getFiles(e) {
		// isUpload = false;
		e = e || window.event;
		//获取file input中的图片信息列表
		var files = e.target.files,
			//验证是否是图片文件的正则
			reg = /image\/.*/i;
		fileType = files[0].type,
		fileName = files[0].name; //如果选择了多张图片，只上传第一张
		console.log(files);

		if (!reg.test(fileType)) {
			alert("你选择的" + fileName + "文件不是图片");
			return;
		} else {
			imgFiles.push(files[0]);
			$(".selectedFileName").text(fileName);
		}
		$("#status").text("上传中...");
		uploadFile.uploadImgFiles(imgFiles,function(urls){
			$("#status").text("上传完毕");
			iconUrl=urls[0];
		});
	}
	function create(){
		var url=window.location.search;
		var class_id=url.substring(url.indexOf("=")+1);
		var class_name=$("#class_name").val();
		$.ajax({
			url: "msq/api/classification",
			type: "POST",
			data: {
				class_name:class_name,
				superiorId:class_id,
				nodeType:"",
				icon:iconUrl
			},
			success: function(d) {
				iconUrl="";
				var fileInput=$("#icon");
				fileInput.after(fileInput.clone().val("")); 
				fileInput.remove();
				imgFiles=[];//清空输入

				getSubClassNow();
				$("#status").text("创建子分类成功,id:"+d.classId);
				
			}
		});

	}
	function handlebarsRender(tplSelector,destSelector,data){
		//用jquery获取模板
		var source   = $(tplSelector).html();
		//预编译模板
		var template = Handlebars.compile(source);
		//匹配json内容
		var html = template(data);
		//输入模板
		$(destSelector).html(html);
	}
	//获取现存的子分类 
	function getSubClassNow(){
		var url=window.location.search;
		var class_id=url.substring(url.indexOf("=")+1);
		$.ajax({
			url:"msq/api/classification/<class_id>/list".replace(/\<\w+\>/,class_id),
			success:function(d){
				console.log(d)
				var modifiedData=d.list.map(function(data){
					if(data.nodeType==1){
						data.href="createImgsOfClass.html?class_id="+data._id;
						return data;
					}else{
						data.href="createSubClassList.html?class_id="+data._id;
						return data;
					}
				});
				handlebarsRender("#list","#listBox",modifiedData);
			}
		});
	}
	//删除分类
	function deleteClass(classId){
		$.ajax({
			url:"msq/api/classification/remove/<class_id>".replace(/\<\w+\>/,classId),
			type:"DELETE",
			success:function(d){
				alert("删除成功");
				getSubClassNow();//刷新
			}
		});
	}
	$("#icon").on("change", getFiles);
	$("#submit").on("click",create);
	$("body").on("click",".deleteButton",function(e){
		e.stopPropagation();
		var id=$(this).data("id");
		deleteClass(id);
	});
	getSubClassNow();
});
